$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sjtixian/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true ,hidden:true},
            { label: '公司id', name: 'branchCompanyId', index: 'branch_company_id', width: 50 ,hidden:true},
            { label: '分公司名称', name: 'branchCompany', index: 'branch_company', width: 140 },
            { label: '网点id', name: 'netPointId', index: 'net_point_id', width: 50 ,hidden:true},
            { label: '网点名称', name: 'netPoint', index: 'net_point', width: 120 },
            { label: '姓名', name: 'realname', index: 'realname', width: 60 },
            { label: '身份证', name: 'idCard', index: 'id_card', width: 120 },
			{ label: '提现人id', name: 'proposerId', index: 'proposer_id', width: 80 ,hidden:true},
			{ label: '提现金额', name: 'money', index: 'money', width: 60 },
			{ label: '提现手续费', name: 'poundage', index: 'poundage', width: 60 ,hidden:true},
			{ label: '提现实际到账', name: 'actualAccount', index: 'actual_account', width: 60 },
			{ label: '提现银行卡号', name: 'bankCardNo', index: 'bank_card_no', width: 60 ,hidden:true},
			{ label: '提现申请时间', name: 'createTime', index: 'create_time', width: 100 },
			{ label: '是否审核', name: 'ifExamine', index: 'if_examine', width: 60 ,
                cellattr:function(rowId, val, rawObject, cm, rdata) {
			       if(rawObject.ifExamine==0) {
                       return "style='color:red;font-weight:bold'";
                   }
                },
                formatter:function(v){
                    if(v==0){
                        return "未审核";
                    }else if(v==1){

                        return "已审核";
                    }else{
                        return v;
                    }
                }
            },
			{ label: '是否通过', name: 'ifPass', index: 'if_pass', width: 80  , hidden:true,
                formatter:function(v){
                    if(v==0){
                        return "不通过";
                    }else if(v==1){
                        return "通过";
                    }else{
                        return v;
                    }
                }},
			{ label: '审核人', name: 'examinerId', index: 'examiner_id', width: 60 },
			{ label: '审核时间', name: 'examineTime', index: 'examine_time', width: 100 },
			{ label: '提现状态', name: 'txState', index: 'tx_state', width: 80 ,
                cellattr:function(rowId, val, rawObject, cm, rdata) {
                    if(rawObject.txState==6) {
                        return "style='color:red;font-weight:bold'";
                    }
                },
                formatter:function(v){
                    if(v=="1"){
                        return "等待审核";
                    }else if(v=="2"){
                        return "审核通过";
                    }else if(v=="3"){
                        return "等待银行处理";
                    }else if(v=="4"){
                        return "已到账";
                    }else if(v=="5"){
                        return "已取消";
                    }else if(v=="6"){
                        return "审核不通过";
                    }
                }},
			{ label: '到账时间', name: 'arrivalMoneyTime', index: 'arrival_money_time', width: 100 },
			{ label: '审核原因', name: 'remark', index: 'remark', width: 80 ,hidden:true},
            { label: '审核', width: 60 ,formatter: function(value, options, row){
                return '<input type="button" class="btn btn-primary" onclick="vm.checkAudit('+row.id+','+row.ifExamine+')" value="审核"/>';
            }}
        ],
		viewrecords: true,
        height: 680,
        rowNum: 20,
		rowList : [20,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
    vm.getBranchCompanyList();
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		sjTixian: {
            branchCompany:null,
            branchCompanyId:null,
            netPoint:null,
            netPointId:null,
            startCreateTime:null,
            endCreateTime:null,
            examineTime:null,
            examinerId:null,
            remark:null
		},
        q:{
            branchCompany:null,
            netPoint:null
        }
	},
	methods: {
		query: function () {
			vm.reload();
		},
		saveOrUpdate: function (event) {
		    if(vm.sjTixian.examineTime != null) {
                vm.sjTixian.examineTime = dateChange(vm.sjTixian.examineTime);
            }
            vm.sjTixian.txState="2";
			$.ajax({
				type: "POST",
			    url: baseURL + "sjtixian/update",
                contentType: "application/json",
			    data: JSON.stringify(vm.sjTixian),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},

        saveOrUpdateNo: function (event) {
		    if(vm.sjTixian.examineTime != null) {
                vm.sjTixian.examineTime = dateChange(vm.sjTixian.examineTime);
            }
            vm.sjTixian.txState="6";
            $.ajax({
                type: "POST",
                url: baseURL + "sjtixian/update",
                contentType: "application/json",
                data: JSON.stringify(vm.sjTixian),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(index){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sjtixian/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "sjtixian/info/"+id, function(r){
                vm.sjTixian = r.sjTixian;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{'branchCompany': vm.q.branchCompany,'netPoint':vm.q.netPoint,
                    'realname':vm.sjTixian.realname,'startCreateTime':dateChange(vm.sjTixian.startCreateTime),'endCreateTime':dateChange(vm.sjTixian.endCreateTime)},
                page:page
            }).trigger("reloadGrid");
		},
		empty: function () {
            vm.sjTixian.branchCompany = null;
            vm.sjTixian.branchCompanyId = null;
            vm.sjTixian.netPoint = null;
            vm.sjTixian.netPointId = null;
            vm.q.branchCompany = null;
            vm.q.netPoint = null;
            vm.sjTixian.realname = null;
            vm.sjTixian.startCreateTime = null;
            vm.sjTixian.endCreateTime = null;
            vm.reload();
        },
        checkAudit:function(id,ifExamine){
		    if(ifExamine==1){
		        alert("此提现已通过审核！");
            }else{
                vm.showList = false;
                vm.title = "提现审核";
                vm.getInfo(id);
            }
        },


        BranchCompanySelect:function(){
            utils.openDialogView('分公司列表',$("#windowBranchCompany"),'900px','500px',1);
        },
        getBranchCompanyList: function(){
            $("#jqGridBranchCompany").jqGrid({
                url: baseURL + 'psbranchcompany/list',
                datatype: "json",
                colModel: [
                    { label: 'id', name: 'id', index: 'id', width: 50, key: true ,hidden:true},
                    { label: '分公司名称', name: 'companyName', index: 'company_name', width: 100 },
                    { label: '分公司营业执照', name: 'companyLicenceImg', index: 'company_licence_img', width: 80 ,hidden:true},
                    { label: '分公司地址', name: 'address', index: 'address', width: 150 },
                    { label: '联系电话', name: 'contactNumber', index: 'contact_number', width: 80 },
                    { label: '联系人', name: 'contactPeople', index: 'contact_people', width: 80 }
                ],
                viewrecords: true,
                height: 385,
                rowNum: 10,
                rowList : [10,30,50],
                rownumbers: true,
                // rownumWidth: 85,
                autowidth:false,
                multiselect: false,
                pager: "#jqGridPagerBranchCompany",
                jsonReader : {
                    root: "page.list",
                    page: "page.currPage",
                    total: "page.totalPage",
                    records: "page.totalCount"
                },
                prmNames : {
                    page:"page",
                    rows:"limit",
                    order: "order"
                },
                gridComplete:function(){
                    //隐藏grid底部滚动条
                    // $("#jqGridBranchCompany").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
                }
            });
        },
        BranchPointSelect:function(){
            if( vm.sjTixian.branchCompanyId == null ) {
                alert("请先选择一个分公司!");
                return ;
            }
            utils.openDialogView('网点列表',$("#windowNetPoint"),'900px','500px',1);
        },
        getjqGridNetPoint: function(branchCompanyId){
            $("#jqGridNetPoint").jqGrid({
                url: baseURL + 'psnetpoint/list',
                datatype: "json",
                postData:{'branchCompanyId': branchCompanyId},
                colModel: [
                    { label: 'id', name: 'id', index: 'id', width: 50, key: true},
                    { label: '网点名称', name: 'pointName', index: 'point_name', width: 120 },
                    { label: '负责人', name: 'person', index: 'person', width: 120 },
                    { label: '电话', name: 'telphone', index: 'telphone', width: 80 },
                    { label: '所属分公司id', name: 'branchCompanyId', index: 'branch_company_id', width: 80 ,hidden:true},
                    { label: '所属分公司名称', name: 'branchCompany', index: 'branch_company', width: 130 ,hidden:true},
                    { label: '网点地址', name: 'address', index: 'address', width: 300 },
                    { label: '营业时间', name: 'shopHours', index: 'shop_hours', width: 80 ,hidden:true},
                    { label: '收件时间', name: 'receivingTime', index: 'receiving_time', width: 80 ,hidden:true},
                    { label: '经度', name: 'longitude', index: 'longitude', width: 80 ,hidden:true},
                    { label: '纬度', name: 'latitude', index: 'latitude', width: 80 ,hidden:true},
                    { label: '省', name: 'province', index: 'province', width: 80,hidden:true },
                    { label: '市', name: 'city', index: 'city', width: 80 ,hidden:true},
                    { label: '区', name: 'country', index: 'country', width: 80 ,hidden:true},
                    { label: '高德云图id(高德检索用)', name: 'gaoId', index: 'gao_id', width: 80 ,hidden:true}
                ],
                viewrecords: true,
                height: 385,
                rowNum: 10,
                rowList : [10,30,50],
                rownumbers: true,
                // rownumWidth: 25,
                autowidth:false,
                multiselect: false,
                pager: "#jqGridPagerNetPoint",
                jsonReader : {
                    root: "page.list",
                    page: "page.currPage",
                    total: "page.totalPage",
                    records: "page.totalCount"
                },
                prmNames : {
                    page:"page",
                    rows:"limit",
                    order: "order"
                },
                gridComplete:function(){
                    //隐藏grid底部滚动条
                    // $("#jqGridNetPoint").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
                }
            });
        }
	}
});

function dateChange(dateTime){
    if(dateTime != null) {
        var seperator1 = "-";
        var year = dateTime.getFullYear();
        var month = dateTime.getMonth() + 1;
        var strDate = dateTime.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        var currentdate = year + seperator1 + month + seperator1 + strDate;
        return currentdate;
    }else{
        return null;
    }
};


/** 分公司弹窗确定和关闭按钮处理 **/
$("#addBranchCompany").click(function(){
    //判断是否选中了数据
    var id=$("#jqGridBranchCompany").jqGrid('getGridParam','selrow');
    if(id==null){
        alert("请选择分公司!");
        return ;
    }
    var rowData = $("#jqGridBranchCompany").jqGrid('getRowData',id);
    vm.sjTixian.branchCompany = rowData.companyName;
    vm.sjTixian.branchCompanyId = rowData.id;
    vm.q.branchCompany = rowData.companyName;

    utils.closeCurrentPage();
    vm.getjqGridNetPoint(vm.sjTixian.branchCompanyId);
});

$("#closeBranchCompany").click(function(){
    utils.closeCurrentPage();
});

/** 网点弹窗口确定和关闭按钮处理 **/
$("#addNetPoint").click(function(){
    //判断是否选中了数据
    var id=$("#jqGridNetPoint").jqGrid('getGridParam','selrow');
    if(id==null){
        alert("请选择网点!");
        return ;
    }
    var rowData = $("#jqGridNetPoint").jqGrid('getRowData',id);
    vm.sjTixian.netPoint = rowData.pointName;
    vm.sjTixian.netPointId = rowData.id;
    vm.q.netPoint = rowData.pointName;

    utils.closeCurrentPage();
});

$("#closeNetPoint").click(function(){
    utils.closeCurrentPage();
});